package com.jiuzhe.commercial.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.StrictMode;
import android.support.multidex.MultiDex;

import com.blankj.utilcode.utils.Utils;
import com.meituan.android.walle.WalleChannelReader;
import com.orhanobut.logger.LogLevel;
import com.orhanobut.logger.Logger;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 *
 */
public class BaseApplication extends Application {

    public static Map<String, Long> map;
    private static Context context;
    private static BaseApplication mInstance;
    /**
     * 主线程ID
     */
    private static int mMainThreadId = -1;
    /**
     * 主线程ID
     */
    private static Thread mMainThread;

    /**
     * 主线程Handler
     */
    private static Handler mMainThreadHandler;

    private static List<Activity> records = new ArrayList<>();

    public static Context getContext() {
        return context;
    }

    public static BaseApplication getApplication() {
        return mInstance;
    }

    /**
     * 获取主线程ID
     */
    public static int getMainThreadId() {
        return mMainThreadId;
    }

    /**
     * 获取主线程
     */
    public static Thread getMainThread() {
        return mMainThread;
    }

    /**
     * 获取主线程的handler
     */
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    public static void perfectExit() {
        for (Activity activity : records) {
            activity.finish();
        }
    }

    /*
     * android应用程序真正入口。
     * 此方法在所有activity，servie，receiver组件之前调用
     * */
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        mMainThreadId = android.os.Process.myTid();
        mMainThread = Thread.currentThread();
        mMainThreadHandler = new Handler();
        mInstance = this;
        Utils.init(this);

        Logger.init("hhh")
                .methodOffset(2)
                .methodCount(2)
                .hideThreadInfo()
                .logLevel(LogLevel.FULL);
        //检测内存
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        //美团多渠道打包
        String channel = WalleChannelReader.getChannel(getApplication());
        Bugly.setAppChannel(getApplication(), channel);

        //Bugly.setIsDevelopmentDevice(this, false);
        //设置为开发设备
       // CrashReport.setIsDevelopmentDevice(this, false);
        Bugly.init(getApplication(), "3e3e513583", false);
        CrashReport.setUserId(getApplication(), "15007201851");
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
        // 安装tinker
        Beta.installTinker();
    }

    public void addActvity(Activity activity) {
        records.add(activity);
    }

    public void removeActvity(Activity activity) {
        records.remove(activity);
    }
}
